package com.gid.rcflightlogger;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableRow;
import android.widget.TextView;


public class AircraftBrowseActivity extends InventoryBrowseBaseActivity {

	private class AircraftData
	{
		public Aircraft mAircraft;
		private int mFlightCount;
		private int mFlightTime; //seconds
		
		AircraftData(Aircraft a)
		{
			mAircraft = a;
			mFlightCount = 0;
			mFlightTime = 0;
			
			//load cycle data from DB
			List<Flight> flights = mAircraft.getFlights(getApplicationContext()).orderBy("mDate").toList();
			for (Flight f : flights)
			{
				mFlightCount += f.getCount();
				mFlightTime += f.getDuration();
			}
		}
		
		public int getFlightCount()
		{
			return mFlightCount;
		}
		
		public int getFlightTime()
		{
			return mFlightTime;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		PREF_PREFIX = "AircraftBrowse";
		super.onCreate(savedInstanceState);
        mCurrentMenuID = R.id.menu_main_inventory_aircrafts;
        LOG_TAG = "AircraftBrowse";
        mAdd.setText("Add Aircraft");
     }
	
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

	// ====================================================
	// Override methods from base inventory browse class
	// ====================================================
	
	@Override
	protected TableRow renderHeaders()
	{
		TableRow header = (TableRow) getLayoutInflater().inflate(R.layout.layout_aircraftbrowse_header, null);
		return header;
	}

	@Override
	protected void spawnItemEditor(int request_code, int id)
	{
		Intent i = new Intent(this, AircraftEditActivity.class);
		if (request_code == REQUEST_CODE_EDIT && id != 0)
    	{
			i.putExtra(Aircraft.ID_LABEL, id);
			//myUtils.makeToast(getApplicationContext(), "id = " + Integer.toString(id));
    	}
		startActivityForResult(i, request_code);
	}

	@Override
	protected void loadData()
	{
		//reload data from database
		List<Aircraft> allaircraft = Aircraft.objects(getApplicationContext()).all().orderBy("-mStatus").toList();
		mData.clear();
		
		for (Aircraft a : allaircraft)
		{
			AircraftData ad = new AircraftData(a);
			mData.add(ad);
		}
		
	}
	
	@Override
	protected int doCompare(Object lhs, Object rhs)
	{
		int res = 0;
		assert(lhs instanceof AircraftData);
		assert(rhs instanceof AircraftData);
		
		AircraftData la =  (AircraftData) lhs;
		AircraftData ra =  (AircraftData) rhs;
		
		if (sortColumn >= 0 && sortColumn < COLUMN_COUNT) {
			if (sortColumn == 0) // name
				res = la.mAircraft.getName().compareTo(ra.mAircraft.getName());
			else if (sortColumn == 1) // age
				res = (-1)*(la.mAircraft.getBuildDate().compareTo(ra.mAircraft.getBuildDate()));
			else if (sortColumn == 2) // flights
				res = la.getFlightCount() - ra.getFlightCount();
			else if (sortColumn == 3) // flight time
				res = la.getFlightTime() - ra.getFlightTime();

			if (sortUp)
				res *= -1;
		}
		else //no sort, use db order
		{
			res = la.mAircraft.getId() - ra.mAircraft.getId();
		}

		return res;
	}

	@Override
	protected TableRow renderRow(Object od)
	{
		TableRow tr;
		TextView tv1, tv2, tv3, tv4;
		AircraftData ad = (AircraftData) od;
		
		tr = (TableRow) getLayoutInflater().inflate(R.layout.layout_aircraftbrowse_row, null);
		tr.setId(ad.mAircraft.getId());

		// Fill out cells
		tv1 = (TextView) tr.findViewById(R.id.aircraftbrowse_col_name);
		tv1.setGravity(Gravity.LEFT);
		tv1.setText(ad.mAircraft.getName());
		
		tv2 = (TextView) tr.findViewById(R.id.aircraftbrowse_col_age);
		tv2.setGravity(Gravity.RIGHT);
		tv2.setText(myUtils.makeAgeStr(ad.mAircraft.getBuildDate()));
		
		tv3 = (TextView) tr.findViewById(R.id.aircraftbrowse_col_flights);
		tv3.setGravity(Gravity.RIGHT);
		tv3.setText(Integer.toString(ad.getFlightCount()));
	    
		tv4 = (TextView) tr.findViewById(R.id.aircraftbrowse_col_flighttime);
		tv4.setGravity(Gravity.RIGHT);
		tv4.setText(myUtils.Duration2Str(ad.getFlightTime(), true)); 
		
		//fade text if the item is deactivated
		if (!ad.mAircraft.getStatus())
		{
			tv1.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv2.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv3.setTextColor(Colors.TEXT_INACTIVE_ITEM);
			tv4.setTextColor(Colors.TEXT_INACTIVE_ITEM);
		}
		
		return tr;
	}
}
